using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;

namespace BF
{
    public static class DateUtil
    {
        public static readonly DateTime DefaultUTC = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        /// <summary>
        /// 获取时间戳
        /// </summary>
        public static long GetTimestamp(this DateTime dateTime)
        {
            if (dateTime.Kind != DateTimeKind.Utc)
            {
                dateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime);
            }
            return Convert.ToInt64((dateTime - DefaultUTC).TotalSeconds);
        }

        public static DateTime ToDateTime(long timestamp)
        {
            return DefaultUTC.AddSeconds(timestamp);
        }

        /// <summary> 
        /// 判断两个日期是否在同一天
        /// </summary> 
        public static bool IsSameDay(DateTime res, DateTime des)
        {
            return res.Date.Equals(des.Date);
        }


        /// <summary> 
        /// 判断两个日期是否在同一周 
        /// </summary> 
        /// <param name="start">开始日期</param> 
        /// <param name="end">结束日期</param>
        /// <returns></returns> 
        public static bool IsSameWeek(DateTime start, DateTime end)
        {
            if (IsSameDay(start, end))
            {
                return true;
            }
            if (start > end)
            {
                return IsSameWeek(end, start);
            }

            TimeSpan daySpan = end.Date - start.Date;
            if (daySpan.TotalDays >= 7)
            {
                return false;
            }
            if (start.DayOfWeek == DayOfWeek.Sunday
                || end.DayOfWeek != DayOfWeek.Sunday && end.DayOfWeek < start.DayOfWeek)
            {
                return false;
            }
            return true;
        }
    }
}
